package com.by.service.impl;

import com.by.bean.CustomerConsultation;
import com.by.bean.CustomerInquiry;
import com.by.bean.CustomerInquiryDetail;
import com.by.common.crud.BaseServiceImpl;
import com.by.common.utils.Assert;
import com.by.common.utils.DateUtils;
import com.by.mapper.CustomerConsultationMapper;
import com.by.mapper.CustomerInquiryDetailMapper;
import com.by.mapper.CustomerInquiryMapper;
import com.by.service.CustomerInquiryService;
import com.by.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CustomerInquiryServiceImpl extends BaseServiceImpl<CustomerInquiry,CustomerInquiryMapper> implements CustomerInquiryService {

    String maxCode; // 最大编码

    @Autowired
    CustomerInquiryMapper inquiryMapper;

    @Autowired
    CustomerInquiryDetailMapper detailMapper;

    @Autowired
    CustomerConsultationMapper consultationMapper;



    @Transactional
    @Override
    public int save(CustomerInquiry entity) {
        //1.生成编码
        entity.setCode(queryMaxCode());
        //2.生成询价单
        int rs = inquiryMapper.save(entity);
        Assert.isZero(rs,"保存询价单失败");
        //3.保存询价明细
        if(null != entity.getCustomerInquiryDetails() && entity.getCustomerInquiryDetails().size()>0){
            //批量保存
            rs = detailMapper.batchSave(entity.getId(),entity.getCustomerInquiryDetails());
            Assert.isZero(rs,"保存询价明细失败");
        }
        return 1;
    }

    @Transactional
    @Override
    public int saveConsultation(CustomerInquiry entity) {
        entity.setCode(queryMaxCode());
        int rs = inquiryMapper.save(entity);
        Assert.isZero(rs,"保存咨询单失败");
        if(null != entity.getCustomerConsultations() && entity.getCustomerConsultations().size()>0){
            rs = consultationMapper.saveConsultation(entity.getId(),entity.getCustomerConsultations());
            Assert.isZero(rs,"保存咨询单失败");
        }
        return 1;
    }

    @Override
    public List<CustomerInquiryDetail> queryById(int id) {
        List<CustomerInquiryDetail> rs = detailMapper.queryById(id);
        return rs;
    }


    public String queryMaxCode(){
        //获取年月
        String ym = DateUtils.getSystemTimeYM();
        //判断最大编码是否存在
        if(null == maxCode){
            //如果不存在，查询数据库
            maxCode = inquiryMapper.queryMaxCode();
            //如果当前没有编码，说明当前的询价单是当月第一笔
            if(null == maxCode){
                //产生新的编码
                maxCode = "XJ"+ym+"0001";
                return maxCode;
            }

        }
        //执行到这里 获取最大编码 XJ25020001 计算新的编码
        //提取编码中的日期 2502
        String tmpYM = maxCode.substring(2,6);
        if(!ym.equals(tmpYM)){
            //说明当前最大编码不是当月的编码
            maxCode = "XJ"+ym+"0001";
            return maxCode;
        }

        //如果执行到这里 说明是当月的最大编码 序号累加XJ25020001
        String prefix = maxCode.substring(0,6);
        String seq = maxCode.substring(6);
        int num = Integer.parseInt(seq);
        num++;
        maxCode = prefix+String.format("%04d",num);
        return maxCode;
    }


}
